package com.sky.utils;

import com.sky.entity.AdminActionLog;
import com.sky.vo.LogVO;
import lombok.extern.slf4j.Slf4j;

import java.text.SimpleDateFormat;
import java.util.Date;
@Slf4j
public class LogConvertUtil {

    /**
     * 将原始日志实体转换为视图对象
     */
    public static LogVO convertToLogVO(AdminActionLog log) {
        LogVO vo = new LogVO();

        // 1. 格式化时间
        vo.setTime(formatLogTime(log.getCreateTime()));

        // 2. 设置类型
        vo.setType(mapActionToType(log.getAction()));

        // 3. 生成内容描述
        vo.setContent(generateLogContent(log));

        return vo;
    }

    /**
     * 格式化日志时间
     */
    private static String formatLogTime(Date createTime) {
        if (createTime == null) return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(createTime);
    }

    /**
     * 映射操作类型到前端样式类型
     */
    private static String mapActionToType(String action) {
        if (action == null) return "default";
        if (action.startsWith("添加")) return "success"; // 修改：添加使用success
        if (action.startsWith("修改")) return "primary"; // 修改：修改使用primary
        if (action.startsWith("删除")) return "danger";
        if (action.startsWith("登录")) return "info";
        return "default";
    }

    /**
     * 生成日志内容描述
     */
    private static String generateLogContent(AdminActionLog actionLog) {
        StringBuilder content = new StringBuilder();
        return content.append("管理员").append(actionLog.getAction()).toString();
    }


}